package com.iespring.controller.admin.auth;

import com.iespring.anno.OperationLog;
import com.iespring.bean.admin.user.bo.ConfigExpressBo;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.admin.user.vo.*;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.config.shiro.MallToken;
import com.iespring.service.admin.AdminAuthService;
import com.iespring.util.Md5Utils;
import com.iespring.util.StrUtil;
import com.iespring.util.ValidationUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * 登录,
 *
 * @author xD
 * @since 2022/12/28 19:26
 */
@RestController
@RequestMapping("admin")
public class AdminAuthController {

    @Resource
    private AdminAuthService adminAuthService;


    /**
     * 整合shiro之后的登录
     *
     * @param map
     * @return
     * @throws Exception
     */
    //安全操作日志
    @OperationLog(action = "登录")
    @RequestMapping("auth/login")
    public BaseRespVo login(@RequestBody Map map) throws Exception {
        //获取前端传入的用户名密码
        String username = (String) map.get("username");
        String originPassWord = (String) map.get("password");
        //通过MD5加密密码
            String password = Md5Utils.getMd5(originPassWord);
        //通过malltoken多携带type用以区分admin和wx
        MallToken admin = new MallToken(username, password, "admin");
        LoginUserVo login = adminAuthService.login(admin);
        if (!ObjectUtils.isEmpty(login)) {
            return BaseRespVo.ok(login);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    /**
     * 用于登录后显示用户信息
     *
     * @since 2022/12/28 21:54
     */
    @RequestMapping("auth/info")
    public BaseRespVo info() {
        AuthInfoVo info = adminAuthService.info();
        if (StrUtil.isEmpty(info)) {
            return BaseRespVo.error("请登录", 501);
        } else {
            return BaseRespVo.ok(info);
        }
    }

    /**
     * 首页
     *
     * @since 2022/12/28 21:54
     */
    @GetMapping("dashboard")
    public BaseRespVo dashboard() {
        DashBoardVo dashBoardVo = adminAuthService.dashboard();
        return BaseRespVo.ok(dashBoardVo);
    }

    /**
     * 用户列表
     *
     * @since 2022/12/29 12:59
     */
    @GetMapping("user/list")
    public BaseRespVo userList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.userList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 修改用户信息
     *
     * @since 2022/12/29 12:59
     */
    @PostMapping("user/update")
    public BaseRespVo userUpdate(@RequestBody @Valid MallUser mallUser, BindingResult bindingResult) {
        //开启数据校验
        ValidationUtils.validParam(bindingResult);
        return adminAuthService.userUpdate(mallUser);
    }

    @GetMapping("address/list")
    public BaseRespVo addressList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.addressList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 临时登出
     *
     * @since 2022/12/28 23:26
     */
    @PostMapping("auth/logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok(0);
    }

    /**
     * 根据id查询
     *
     * @since 2022/12/29 15:54
     */
    @GetMapping("user/detail")
    public BaseRespVo userDetail(@RequestParam("id") Integer id) {
        MallUser mallUser = adminAuthService.userDetail(id);
        return BaseRespVo.ok(mallUser);
    }

    /**
     * 会员收藏页
     *
     * @since 2022/12/29 16:00
     */
    @GetMapping("collect/list")
    public BaseRespVo collectList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.collectList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 会员足迹
     *
     * @since 2022/12/29 16:42
     */
    @GetMapping("footprint/list")
    public BaseRespVo footprintList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.footprintList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 会员搜索历史
     *
     * @since 2022/12/29 17:02
     */
    @GetMapping("history/list")
    public BaseRespVo historyList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.historyList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 意见反馈
     *
     * @since 2022/12/29 17:20
     */
    @GetMapping("feedback/list")
    public BaseRespVo feedbackList(BasePageInfo basePageInfo) {
        CommonData commonData = adminAuthService.feedbackList(basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 用户统计
     *
     * @since 2022/12/29 19:29
     */
    @GetMapping("stat/user")
    public BaseRespVo statUser() {
        StatUserVo statUserVo = adminAuthService.statUser();
        return BaseRespVo.ok(statUserVo);
    }

    /**
     * 订单统计
     *
     * @since 2022/12/29 22:05
     */
    @GetMapping("stat/order")
    public BaseRespVo statOrder() {
        StatUserVo statUserVo = adminAuthService.statOrder();
        return BaseRespVo.ok(statUserVo);
    }

    /**
     * 商品统计
     *
     * @since 2022/12/29 23:43
     */
    @GetMapping("stat/goods")
    public BaseRespVo statGoods() {
        StatUserVo statUserVo = adminAuthService.statGoods();
        return BaseRespVo.ok(statUserVo);
    }

    /**
     * 商场配置
     *
     * @since 2022/12/30 13:42
     */
    // TODO 待合并
    @GetMapping("config/mall")
    public BaseRespVo configMall() {
        ConfigMallVo configMallVo = adminAuthService.configMall();
        return BaseRespVo.ok(configMallVo);
    }

    @PostMapping("config/mall")
    public BaseRespVo configMall(@RequestBody @Valid ConfigMallVo configMallVo, BindingResult bindingResult) {
        //开启数据校验
        ValidationUtils.validParam(bindingResult);
        adminAuthService.updateConfigMall(configMallVo);
        return BaseRespVo.ok();
    }

    @GetMapping("config/express")
    public BaseRespVo configExpress() {
        ConfigExpressVo configExpressVo = adminAuthService.configExpress();
        return BaseRespVo.ok(configExpressVo);
    }

    @PostMapping("config/express")
    public BaseRespVo configExpress(@RequestBody @Valid ConfigExpressBo configExpressBo, BindingResult bindingResult) {
        //开启数据校验
        ValidationUtils.validParam(bindingResult);
        adminAuthService.updateConfigExpress(configExpressBo);
        return BaseRespVo.ok();
    }

    @GetMapping("auth/401")
    public BaseRespVo auth401(HttpSession session) {
        return BaseRespVo.ok(0);
    }

    @GetMapping("profile/nnotice")
    public BaseRespVo nnotice() {
        return BaseRespVo.ok(0);
    }
}
